<template>
  <van-icon
    @click="onClickCollected"
    :color="value?'#3296fa':'#777'"
    :name="value?'star':'star-o'"
  />
</template>

<script>
import { favoriteArticles, removeArticles } from '@/api/article'
import { Toast } from 'vant'

export default {
  name: 'ArticleCollected',
  props: {
    value: {
      type: Boolean,
      required: true
    },
    artId: {
      type: [Number, Object],
      required: true
    }
  },
  methods: {
    async onClickCollected () {
      try {
        if (this.value) {
          // 取消收藏
          await removeArticles(this.artId)
        } else {
          // 收藏
          await favoriteArticles(this.artId)
        }
        Toast.success('成功')
        this.$emit('input', !this.value)
      } catch (e) {
        Toast.fail('网络异常')
      }
    }

  }
}
</script>

<style scoped lang='less'>

</style>
